#!/usr/bin/env python
# Create: 2018/10/24
# __author__ = '749B'
"""socket HTTP/1.1"""

import socket
from bs4 import BeautifulSoup

client = socket.socket()
# 连接
client.connect(('edu.51cto.com', 80))
# 发送
header = b'GET / HTTP/1.1\r\nHost: edu.51cto.com\r\n\r\n'
client.sendall(header)
# 接收
data = client.recv(1024)
body = b''

# 获取请求头
while len(data.split(b'\r\n\r\n', 1)) != 2:
    data += client.recv(1024)
head, data = data.split(b'\r\n\r\n', 1)
print('HEAD', head)

# 拼接body
while data != b'0\r\n\r\n':
    while len(data.split(b'\r\n', 1)) != 2:
        data += client.recv(1024)

    l, b = data.split(b'\r\n', 1)
    length = int(l, base=16)
    if len(b) <= length:
        body += b
        length -= len(b)
        # 一下子把整段数据剩余的部分都读完
        # length 可能会很长，但是可能一次收不全，所以得用循环和计数直到收完
        while length:
            b = client.recv(length)
            body += b
            length -= len(b)
        data = b''
    else:
        body += b[:length]
        data = b[length:]

    # 把下一段body开头的 b'\r\n' 切掉
    while len(data) < 2:
        data += client.recv(1024)
    else:
        # 这个断言可以验证之前的逻辑是否有问题
        assert data[0:2] == b'\r\n', b'data error: %d %b' % (len(data), data)
        data = data[2:]

print(len(body), body)
soup = BeautifulSoup(body.decode(), features='html.parser')
title = soup.find('title')
print(title)
